using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Globalization;

namespace Atechnology.glassCAD.Shapes.CutViewer
{
    public class Tra3Parser : IParser
    {
        private List<string> lines;

        private CutModel[] models;

        public CutModel[] Models
        {
            get { return models; }
            set { models = value; }
        }

        #region IParser Members

        public CutModel Parse(string filePath)
        {
            Stream str = File.Open(filePath, FileMode.Open);
            CutModel result = this.Parse(str);
            str.Close();
            return result;
        }

        public CutModel Parse(System.IO.Stream stream)
        {
            CutModel result = new CutModel();
            StreamReader reader = new StreamReader(stream);
            lines = new List<string>();
            string line = "";
            while (true)
            {
                line = reader.ReadLine();
                if (line == null) break;

                lines.Add(line);
            }
            models = new CutModel[lines.Count];
            for(int i =0; i < lines.Count; i++)
            {                
                List<IGeometry> lineList = this.ParseLine(lines[i]);
                int index = this.ParseAnzIndex(lines[i]) - 1;
                models[index] = new CutModel();
                models[index].Figures.AddRange(lineList);
            }
            reader.Close();
            return models[0];            
        }

        #endregion

        private int ParseAnzIndex(string tra3Line)
        {
            string indexString = tra3Line.Substring(28, 4);
            int index = int.Parse(indexString);
            return index;
        }

        private List<IGeometry> ParseLine(string tra3Line)
        {
            List<IGeometry> lineList = new List<IGeometry>();
            string width;
            string height;
            CultureInfo en = new CultureInfo("en-US");
            SizeF[] sizes = new SizeF[2];
            for (int i = 0; i < 2; i++)
            {
                width = tra3Line.Substring(10 * i + 4, 4) + "." + tra3Line.Substring(10 * i + 8, 1);
                height = tra3Line.Substring(10 * i + 9, 4) + "." + tra3Line.Substring(10 * i + 13, 1);
                sizes[i] = new SizeF(float.Parse(width, en), float.Parse(height, en));                
            }
            PointF[] points = new PointF[2];

            points[0] = new PointF(0, 0);
            points[1] = new PointF(sizes[0].Width, 0);
            lineList.Add(new Line(points));

            points = new PointF[2];
            points[0] = new PointF(sizes[0].Width, 0);
            points[1] = new PointF(sizes[0].Width, sizes[0].Height);
            lineList.Add(new Line(points));

            points = new PointF[2];
            points[0] = new PointF(sizes[1].Width, sizes[1].Height);
            points[1] = new PointF(0, sizes[1].Height);
            lineList.Add(new Line(points));

            points = new PointF[2];
            points[0] = new PointF(0, sizes[1].Height);
            points[1] = new PointF(0, 0);
            lineList.Add(new Line(points));

            return lineList;
        }
    }
}
